package com.sleep.drug_traceability.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sleep.drug_traceability.dao.mapper.PerfectManMapper;
import com.sleep.drug_traceability.dao.mapper.PerfectSelMapper;
import com.sleep.drug_traceability.dao.mapper.PerfectSupMapper;
import com.sleep.drug_traceability.dao.pojo.ManInfo;
import com.sleep.drug_traceability.dao.pojo.SelInfo;
import com.sleep.drug_traceability.dao.pojo.SupInfo;
import com.sleep.drug_traceability.dao.pojo.User;
import com.sleep.drug_traceability.service.LoginService;
import com.sleep.drug_traceability.service.PerfectService;
import com.sleep.drug_traceability.service.UserService;
import com.sleep.drug_traceability.vo.ErrorCode;
import com.sleep.drug_traceability.vo.Result;
import com.sleep.drug_traceability.vo.params.ManParam;
import com.sleep.drug_traceability.vo.params.SelParam;
import com.sleep.drug_traceability.vo.params.SupParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sleep-6000
 * @date 2022/4/25
 */
@Service
@Transactional
public class PerfectServiceImpl implements PerfectService {

    @Autowired
    private UserService userService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private PerfectManMapper perfectManMapper;
    @Autowired
    private PerfectSelMapper perfectSelMapper;
    @Autowired
    private PerfectSupMapper perfectSupMapper;

    @Override
    public Result perfectManInfo(ManParam manParam, String token) {
        User user = loginService.checkToken(token);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        ManInfo manInfo = new ManInfo();
        manInfo.setName(manParam.getName());
        manInfo.setLegal(manParam.getLegal());
        manInfo.setAddress(manParam.getAddress());
        manInfo.setUscc(manParam.getUscc());
        manInfo.setContact(manParam.getContact());
        manInfo.setUserId(user.getId());
        manInfo.setCreateDate(System.currentTimeMillis());
        manInfo.setLastDate(System.currentTimeMillis());

        perfectManMapper.insert(manInfo);
        userService.updateIsPerfect(user);

        return Result.success("success perfect!");
    }

    @Override
    public Result perfectSelInfo(SelParam selParam, String token) {
        User user = loginService.checkToken(token);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        SelInfo selInfo = new SelInfo();
        selInfo.setName(selParam.getName());
        selInfo.setType(selParam.getType());
        selInfo.setLegal(selParam.getLegal());
        selInfo.setIdNumber(selParam.getIdNumber());
        selInfo.setAddress(selParam.getAddress());
        selInfo.setUserId(user.getId());
        selInfo.setCreateDate(System.currentTimeMillis());
        selInfo.setLastDate(System.currentTimeMillis());

        perfectSelMapper.insert(selInfo);
        userService.updateIsPerfect(user);

        return Result.success("success perfect!");
    }

    @Override
    public Result perfectSupInfo(SupParam supParam, String token) {
        User user = loginService.checkToken(token);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        SupInfo supInfo = new SupInfo();
        supInfo.setName(supParam.getName());
        supInfo.setManager(supParam.getManager());
        supInfo.setAddress(supParam.getAddress());
        supInfo.setUserId(user.getId());
        supInfo.setCreateDate(System.currentTimeMillis());
        supInfo.setLastDate(System.currentTimeMillis());

        perfectSupMapper.insert(supInfo);
        userService.updateIsPerfect(user);

        return Result.success("success perfect!");
    }

    @Override
    public void changeLegal(Long id, String newLegal) {
        LambdaUpdateWrapper<ManInfo> wrapper = new LambdaUpdateWrapper<>();
        ManInfo manInfo = new ManInfo();
        manInfo.setLegal(newLegal);
        manInfo.setLastDate(System.currentTimeMillis());
        wrapper.eq(ManInfo::getUserId, id);
        perfectManMapper.update(manInfo, wrapper);
    }

    @Override
    public void changeContact(Long id, String newContact) {
        LambdaUpdateWrapper<ManInfo> wrapper = new LambdaUpdateWrapper<>();
        ManInfo manInfo = new ManInfo();
        manInfo.setContact(newContact);
        manInfo.setLastDate(System.currentTimeMillis());
        wrapper.eq(ManInfo::getUserId, id);
        perfectManMapper.update(manInfo, wrapper);
    }

    @Override
    public List<String> getAllSellerName() {
        List<SelInfo> selInfos = perfectSelMapper.selectList(new LambdaQueryWrapper<>());
        List<String> buyers = new ArrayList<>();
        for (SelInfo selInfo : selInfos) {
            buyers.add(selInfo.getName());
        }
        return buyers;
    }

    @Override
    public String getManName(Long id) {
        LambdaQueryWrapper<ManInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManInfo::getUserId, id);
        wrapper.last(" limit 1 ");
        ManInfo manInfo = perfectManMapper.selectOne(wrapper);
        return manInfo.getName();
    }

    @Override
    public String getSelName(Long id) {
        LambdaQueryWrapper<SelInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SelInfo::getUserId, id);
        wrapper.last(" limit 1 ");
        SelInfo selInfo = perfectSelMapper.selectOne(wrapper);
        return selInfo.getName();
    }

    @Override
    public String getSupName(Long id) {
        LambdaQueryWrapper<SupInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SupInfo::getUserId, id);
        wrapper.last(" limit 1 ");
        SupInfo supInfo = perfectSupMapper.selectOne(wrapper);
        return supInfo.getName();
    }
}
